package com.yidu.qx.service;

import com.yidu.qx.action.vo.UserEntityVo;
import com.yidu.qx.action.vo.UserVo;
import com.yidu.qx.dao.CharacterDao;
import com.yidu.qx.dao.RoleUserDao;
import com.yidu.qx.dao.UserDao;
import com.yidu.qx.domain.QxCharacterEntity;
import com.yidu.qx.domain.QxRoleUserEntity;
import com.yidu.qx.domain.QxUserEntity;
import com.yidu.qx.utils.Pages;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @program: jurisdiction
 * @description
 * @author: 周佳佳
 * @create: 2019-09-25 15:13
 **/
public class UserImpl implements  UserService {
    Logger logger= (Logger) Logger.getLogger(String.valueOf(UserImpl.class));
    private UserDao userDao;
    private CharacterDao characterDao;
    private RoleUserDao roleUserDao;

    public RoleUserDao getRoleUserDao() {
        return roleUserDao;
    }

    public void setRoleUserDao(RoleUserDao roleUserDao) {
        this.roleUserDao = roleUserDao;
    }

    public CharacterDao getCharacterDao() {
        return characterDao;
    }

    public void setCharacterDao(CharacterDao characterDao) {
        this.characterDao = characterDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    public void add(UserEntityVo userEntityVo) {
        QxUserEntity qxUserEntity=new QxUserEntity();
        Date utilDate = new Date();//util.Date
        System.out.println("utilDate : " + utilDate);
        //util.Date转sql.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        System.out.println("sqlDate : " + sqlDate);
        userEntityVo.setUserFoundDate(sqlDate);
        userEntityVo.setUserFoundUser("admin");
        BeanUtils.copyProperties(userEntityVo,qxUserEntity);
        userDao.add(qxUserEntity);
        if(userEntityVo.getChaId()!=null &&!"".equals(userEntityVo.getChaId())){
            String[] id=userEntityVo.getChaId().split(",");
            for (int i = 0; i < id.length; i++) {
                String ids= id[i];
                if(StringUtils.isEmpty(ids)) {
                    continue ;
                }
                QxCharacterEntity qxCharacterEntity= characterDao.findById(id[i]);
                QxRoleUserEntity qxRoleUserEntity=new QxRoleUserEntity();
                qxRoleUserEntity.setQxCharacterEntity(qxCharacterEntity);
                qxRoleUserEntity.setQxUserEntity(qxUserEntity);
                roleUserDao.add(qxRoleUserEntity);
            }
        }
    }
    @Override
    public void update(UserEntityVo userEntityVo) {
        if(userEntityVo.getUserId()!=null&&!"".equals(userEntityVo.getUserId())){
            QxUserEntity qxUserEntity=userDao.findById(userEntityVo.getUserId());
            Date utilDate = new Date();//util.Date
            //util.Date转sql.Date
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            userEntityVo.setUserFoundDate(sqlDate);
            userEntityVo.setUserFoundUser("admin");
            BeanUtils.copyProperties(userEntityVo,qxUserEntity);
            userDao.update(qxUserEntity);
            List<QxRoleUserEntity> list=roleUserDao.findUserId(qxUserEntity.getUserId());
            for (int i = 0; i < list.size(); i++) {
                roleUserDao.delete(list.get(i).getRuId());
            }
            String[] id=userEntityVo.getChaId().split(",");
            for (int i = 0; i < id.length; i++) {
                String ids= id[i];
                if(StringUtils.isEmpty(ids)) {
                    continue ;
                }
                QxCharacterEntity qxCharacterEntity= characterDao.findById(id[i]);
                QxRoleUserEntity qxRoleUserEntity=new QxRoleUserEntity();
                qxRoleUserEntity.setQxCharacterEntity(qxCharacterEntity);
                qxRoleUserEntity.setQxUserEntity(qxUserEntity);
                roleUserDao.add(qxRoleUserEntity);
            }
        }

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void update(QxUserEntity qxUserEntity) {
        userDao.update(qxUserEntity);
    }


    @Override
    public List<UserEntityVo> findAll(UserEntityVo userEntityVo, Pages pages) {
        QxUserEntity qxUserEntity=new QxUserEntity();
        BeanUtils.copyProperties(userEntityVo, qxUserEntity);
        List<UserEntityVo> lista=new ArrayList<>();
        List<QxUserEntity> list=userDao.findAll(qxUserEntity,pages);
        for (Iterator<QxUserEntity> iterator = list.iterator(); iterator.hasNext(); ) {
            QxUserEntity next = iterator.next();
            UserEntityVo v=new UserEntityVo();
            BeanUtils.copyProperties(next,v);
            lista.add(v);
        }
        return lista;
    }

    @Override
    public QxUserEntity findById(String id) {
        QxUserEntity qxUserEntity=userDao.findById(id);
        return qxUserEntity;
    }
    @Override
    public  List<QxUserEntity> findName(String userName) {
        List<QxUserEntity> qxUserEntity=userDao.findName(userName);
        return qxUserEntity;
    }
    @Override
    public Long count(UserEntityVo userEntityVo) {
        QxUserEntity qxUserEntity1=new QxUserEntity();
        BeanUtils.copyProperties(userEntityVo,qxUserEntity1);
        return userDao.count(qxUserEntity1);
    }
    @Override
    public UserEntityVo findByIdThree(String id) {
        QxUserEntity qxUserEntity=userDao.findByIdThree(id);
        UserEntityVo userEntityVo=new UserEntityVo();
        BeanUtils.copyProperties(qxUserEntity,userEntityVo);
        List<QxRoleUserEntity> list=roleUserDao.findUserId(userEntityVo.getUserId());
        String ids="";
        for (int i = 0; i < list.size(); i++) {
            ids+=list.get(i).getQxCharacterEntity().getChaId()+",";
        }
        userEntityVo.setChaId(ids);
        return userEntityVo;
    }

    @Override
    public UserVo find(String userName, String userPassword) {
        QxUserEntity po = userDao.find(userName, userPassword);

        if(po!=null) {
            UserVo userVo = new UserVo();
            userVo.setUserName(po.getUserName());
            userVo.setUserPassword(po.getUserPassword());
            BeanUtils.copyProperties(po, userVo);
            return userVo;
        }

        return null;
    }

}
